<template>
    <div style="width: 500px">
        <el-input
                prefix-icon="el-icon-search"
                placeholder="输入部门名称进行搜索"
                v-model="filterText">
        </el-input>

        <el-tree
                :data="deps"
                :props="defaultProps"
                :filter-node-method="filterNode"
                :expand-on-click-node="false"
                ref="tree">
            <span class="custom-tree-node"  slot-scope="{ node, data }">
            <span>{{ data.name }}</span>
            <span>
              <el-button class="depBtn"
                      type="primary"
                      size="mini"
                      @click="() => showAddDepView(data)">
                添加部门
              </el-button>
              <el-button
                      class="depBtn"
                      type="danger"
                      size="mini"
                      @click="() => deleteDep(node, data)">
                删除部门
              </el-button>
            </span>
          </span>
        </el-tree>
        <el-dialog
                title="添加部门"
                :visible.sync="dialogVisible"
                width="30%"
                >
            <div>
                <table>
                    <tr>
                        <td><el-tag>上级部门名称</el-tag></td>
                        <td>{{pname}}</td>
                    </tr>
                    <tr>
                        <td><el-tag>添加部门名称</el-tag></td>
                        <td><input v-model="dep.name" placeholder="填写新增部门名称"></input></td>
                    </tr>
                </table>

            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doAddDep">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {deleteRequest} from "../../../utils/api";
//default-expand-all 默认展开所有
    export default {
        name: "DepMana",
        data() {
            return{
                dialogVisible:false,
                dep:{
                    name:'',
                    parentId:-1
                },
                pname:'',
                filterText:'',
                deps:[],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                }
            }
        },
        //监控器
        watch: {
            //监听input中的变化，:filter-node-method="filterNode"，会到filterNode方法
            filterText(val) {
                this.$refs.tree.filter(val);
            }
        },
        mounted() {
            this.initDeps();
        },
        methods: {
            showAddDepView(data) {
                this.pname = data.name;
                this.dep.parentId = data.id;
                console.log(data);
                this.dialogVisible= true;

            },
            doAddDep() {
                this.postRequest("/system/basic/department/",this.dep).then(resp =>{
                    if (resp) {
                        this.dialogVisible = false;
                        // 添加完，将添加内容置空
                        this.innitDep();
                        //刷新添加的部分
                        this.addDep2Deps(this.deps, resp.obj);
                    }
                })
            },
            innitDep() {
                this.dep = {
                    name:'',
                    parentId:-1
                }
                this.pname = ''
            },
            // ****动态修改数组中的元素，实现刷新。
            addDep2Deps(deps,dep) {
                for (let i = 0; i < deps.length; i++) {
                    let d = deps[i];
                    if (d.id == dep.parentId) {
                        // 返回一个新的数组。该数组是通过把所有 arrayX 参数添加到 arrayObject 中生成的。
                        // 如果要进行 concat() 操作的参数是数组，那么添加的是数组中的元素，而不是数组
                        d.children = d.children.concat(dep);
                        if (d.children.length > 0) {
                            d.parent = true;
                        }
                        return;
                    } else {
                        //递归
                        this.addDep2Deps(d.children, dep);
                    }
                }
            },
            deleteDep(node,data) {
                console.log(node);
                if (data.parent) {
                    this.$message.error("含有下级，无法删除");
                    return;
                }
                this.$confirm('此操作将永久删除该【'+data.name+'】角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/department/"+data.id).then(resp =>{
                        if (resp) {
                            this.removeDepFromDeps(null,this.deps, data.id);
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            // 递归移除
            removeDepFromDeps(p,deps, id) {
                for (let i = 0; i < deps.length; i++) {
                    let dep = deps[i];
                    if (dep.id == id) {
                        //删除元素,从第i 开始删，删一个
                        deps.splice(i,1);
                        //解决 删除完子后，不刷新时，夫部门仍然不允许删除。
                        //删除完成后，父部们标识应该改成false
                        if (deps.length == 0) {
                            p.parent = false;
                        }
                        return;
                    } else {
                        //如果不是在外侧的，看看是否在children里
                        this.removeDepFromDeps(dep,dep.children, id);
                    }
                }

            },
            initDeps() {
                this.getRequest("/system/basic/department/").then(resp =>{
                    if (resp) {
                        this.deps = resp;
                    }

                })
            },
            //Value input 中输入的最新值，data 返回的json
            filterNode(value, data) {
                //没有搜索，保留data
                if (!value) return true;
                return data.name.indexOf(value) !== -1;
            }
        }
    }
</script>

<style>
    .depBtn {
        padding: 2px;
    }
    /*向两边走*/
    .custom-tree-node {
          width: 100%;
          display: flex;
          justify-content: space-between;
      }
</style>
